package com.apptest.android.ui.architecture;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.apptest.android.R;
import com.apptest.android.core.Constants;
import com.apptest.android.core.bean.ArchitectureBean;
import com.apptest.android.core.bean.SubordinateTitleBean;
import com.apptest.android.databinding.FragmentArchitectureBinding;

import java.util.ArrayList;
import java.util.List;

public class ArchitectureFragment extends Fragment {

    private ArchitectureViewModel architectureViewModel;
    private FragmentArchitectureBinding binding;
    private ArchitectureAdapter architectureAdapter;
    StaggeredGridLayoutManager layoutManager;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        architectureViewModel =
                new ViewModelProvider(this).get(ArchitectureViewModel.class);

        binding = FragmentArchitectureBinding.inflate(inflater, container, false);
        layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        initView();
        initRefreshLayout();
        return binding.getRoot();
    }

    private void initView(){
        architectureAdapter = new ArchitectureAdapter(R.layout.item_architecture_block,
                architectureViewModel.getArchitectureList().getValue());
        binding.architectureRecyclerView.setAdapter(architectureAdapter);
        architectureAdapter.setOnItemClickListener((adapter, view, position) -> {
            startArchitectureSubordinateActivity(position);
        });
        binding.architectureRecyclerView.setLayoutManager(layoutManager);
        architectureViewModel.getArchitectureList().observe(getViewLifecycleOwner(),
                list->{
                    architectureAdapter.replaceData(list);
                });
    }

    private void initRefreshLayout() {
        binding.architectureRefreshLayout.setOnRefreshListener(refreshLayout -> {
            architectureViewModel.updateArchitectureList();
            refreshLayout.finishRefresh();
        });
    }

    private void startArchitectureSubordinateActivity(int position){
        int mAdapterSize=architectureAdapter.getData().size();
        if (mAdapterSize <= 0 || mAdapterSize < position) {
            return;
        }
        ArrayList<SubordinateTitleBean> resList = new ArrayList<>();
        ArchitectureBean sourceBean = architectureAdapter.getData().get(position);
        List<ArchitectureBean> architectureList = sourceBean.getChildren();
        for(ArchitectureBean bean: architectureList){
            SubordinateTitleBean titleBean = new SubordinateTitleBean(bean.getId(), bean.getName());
            resList.add(titleBean);
        }
        Bundle bundle = new Bundle();
        bundle.putString(Constants.TITLE,sourceBean.getName());
        bundle.putParcelableArrayList(Constants.SUBORDINATE_TITLE_LIST, resList);
        ArchitectureSubordinateActivity.startArchitectureSubordinateActivity(getContext(), bundle);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

}